package org.jsola.hr.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.jsola.core.Page;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.CustomCellWriteHandler;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceDataTempDAO;
import org.jsola.hr.dao.IAttendanceSallaryTemplateItemDAO;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.AttendanceDataTempDO;
import org.jsola.hr.entity.AttendanceSallaryTemplateItemDO;
import org.jsola.hr.entity.EmpInfoDO;
import org.jsola.hr.exception.HrException;
import org.jsola.hr.provider.ISalaryProviderService;
import org.jsola.hr.query.*;
import org.jsola.hr.service.*;
import org.jsola.hr.service.excel.AttendPunchNoModelDataListener;
import org.jsola.hr.service.excel.AttendPunchSalaryNoModelDataListener;
import org.jsola.hr.vo.*;
import org.jsola.salary.entity.EmployeeSalaryDO;
import org.jsola.salary.query.EmployeeSalaryQuery;
import org.jsola.salary.service.IEmployeeSalaryService;
import org.jsola.salary.vo.AttendancePayImportVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.service.IUserService;
import org.jsola.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 临时考勤数据
 *
 * @author zhr
 */
@Service("hrAttendanceDataTempServiceImpl")
public class AttendanceDataTempServiceImpl implements IAttendanceDataTempService {

    @Autowired
    private IAttendanceDataTempDAO attendanceDataTempDAO;
    @Autowired
    private IEmpInfoService empInfoService;
    @Autowired
    private IPositionInfoService positionInfoService;
    @Autowired
    private IEmpPositionService empPositionService;
    @Autowired
    private IAttendanceDataRecordService attendanceDataRecordService;
    @Autowired
    private IAttendanceSallaryTemplateItemService attendanceSallaryTemplateItemService;
    @Autowired
    private IAttendanceSallaryTemplateService attendanceSalaryTemplateService;
    @Autowired
    private IAttendanceSallaryTemplateItemDAO attendanceSallaryTemplateItemDAO;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISalaryProviderService salaryProviderService;
    @Autowired
    private IEmployeeSalaryService employeeSalaryService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer importMonthSallary(AttendanceDataTempQuery attendanceDataTempQuery11, TokenUser tokenUser) {
        String recordId = attendanceDataTempQuery11.getRecordId();
        String salaryTemplateSalaryIds = attendanceDataTempQuery11.getSalaryTemplateSalaryIds();
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(recordId, tokenUser.getSiteId());
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(0);
        List<AttendancePayImportVO> attendancePayImportVOS = new ArrayList<>();
        List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
        if (select == null || select.size() == 0) {
            throw new ParamException("无法匹配员工信息");
        }
        for (AttendanceDataTempListVO attendanceDataTempListVO : select) {
            AttendancePayImportVO attendancePayImportVO = new AttendancePayImportVO();
            attendancePayImportVO.setEmpId(attendanceDataTempListVO.getEmpId());
            attendancePayImportVO.setAttendancePay(attendanceDataTempListVO.getSallaryImpact());
            attendancePayImportVOS.add(attendancePayImportVO);
        }
        List<String> ids = salaryProviderService.updateAttendancePay(Long.parseLong(salaryTemplateSalaryIds), attendanceDataRecordVO.getCompanyId(),
                attendanceDataRecordVO.getYear(),
                attendanceDataRecordVO.getMonth(),
                attendancePayImportVOS, tokenUser);
        for (String id : ids) {
            AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
            attendanceDataTempUpdateDTO.setId(id);
            attendanceDataTempUpdateDTO.setIsHandle(true);
            update(attendanceDataTempUpdateDTO, tokenUser);
        }
        AttendanceDataRecordUpdateDTO attendanceDataRecordUpdateDTO = new AttendanceDataRecordUpdateDTO();
        attendanceDataRecordUpdateDTO.setIsHandle(true);
        attendanceDataRecordUpdateDTO.setId(recordId);
        attendanceDataRecordUpdateDTO.setSalaryTemplateSalaryId(attendanceDataTempQuery11.getSalaryTemplateSalaryIds());
        attendanceDataRecordService.update(attendanceDataRecordUpdateDTO, tokenUser);
        return ids.size();
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer importMonthSalary(AttendanceDataTempQuery attendanceDataTempQuery, TokenUser tokenUser) {
        String recordId = attendanceDataTempQuery.getRecordId();
        String salaryTemplateSalaryIds = attendanceDataTempQuery.getSalaryTemplateSalaryIds();
        attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(0);
        List<AttendancePayImportVO> attendancePayImportVOList = new ArrayList<>();
        List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(select)) {
            throw new ParamException("无法匹配员工信息");
        }
        for (AttendanceDataTempListVO attendanceDataTempListVO : select) {
            AttendancePayImportVO attendancePayImportVO = new AttendancePayImportVO();
            attendancePayImportVO.setEmpId(attendanceDataTempListVO.getEmpId());
            attendancePayImportVO.setAttendancePay(attendanceDataTempListVO.getSallaryImpact());
            attendancePayImportVOList.add(attendancePayImportVO);
        }
        List<String> empIds = salaryProviderService.updateAttendancePay(Long.parseLong(salaryTemplateSalaryIds)
                , attendancePayImportVOList, tokenUser);
        // todo 修改hr_attendance_data_temp表is_handle字段为true 可根据attendance_data_record_id + emp_id查询唯一?
//        for (String empId : empIds) {
//            AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
//            attendanceDataTempUpdateDTO.setId(empId);
//            attendanceDataTempUpdateDTO.setIsHandle(true);
//            update(attendanceDataTempUpdateDTO, tokenUser);
//        }
        AttendanceDataRecordUpdateDTO attendanceDataRecordUpdateDTO = new AttendanceDataRecordUpdateDTO();
        attendanceDataRecordUpdateDTO.setIsHandle(true);
        attendanceDataRecordUpdateDTO.setId(recordId);
        attendanceDataRecordUpdateDTO.setSalaryTemplateSalaryId(salaryTemplateSalaryIds);
        attendanceDataRecordService.update(attendanceDataRecordUpdateDTO, tokenUser);
        return empIds.size();
    }

    @Override
    public AttendanceDataTempVO errorDataExcelByRecordId(String recordId, TokenUser tokenUser) {
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(recordId, tokenUser.getSiteId());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //查错误数据
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(1);
        List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
        //数据
        List<List<String>> dataList = new ArrayList<>();
        for (int i = 0; i < select.size(); i++) {
            AttendanceDataTempListVO attendanceDataTempListVO = select.get(i);
            List<String> dataInfoList = new ArrayList<>();
            List<String> strings = JSON.parseArray(attendanceDataTempListVO.getValue(), String.class);
            dataInfoList.add(attendanceDataTempListVO.getErrorReason());
            for (String string : strings) {
                if ("null".equals(string)) {
                    string = " ";
                }
                dataInfoList.add(string);
            }
            dataList.add(dataInfoList);
        }

        //制作表头
        List<List<String>> list = new ArrayList<>();
        for (int i = 0; i < 63; i++) {
            ArrayList<String> list1 = new ArrayList<>();
            if (i == 0) {
                list1.add("错误原因");
            } else if (i == 1) {
                list1.add("姓名");
            } else if (i == 2) {
                list1.add("手机");
            } else {
                if (i % 2 == 0) {
                    list1.add("工种");
                } else {
                    list1.add("考勤状态");
                }
            }
            list.add(list1);
        }

        EasyExcel.write(outputStream)
                .head(list)
                .registerWriteHandler(new CustomCellWriteHandler())
                .sheet("考勤错误记录")
                .doWrite(dataList);
        InputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
        AttendanceDataTempVO attendanceDataTempVO = new AttendanceDataTempVO();
        attendanceDataTempVO.setIs(byteArrayInputStream);
        attendanceDataTempVO.setFileName(attendanceDataRecordVO.getFileName());
        return attendanceDataTempVO;
    }

    @Override
    public AttendanceDataTempVO getErrorDataExcel(String recordId, TokenUser tokenUser) {
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(recordId, tokenUser.getSiteId());
        String attendanceSallaryTemplateId = attendanceDataRecordVO.getAttendanceSallaryTemplateId();
        //查所有配置项
        AttendanceSallaryTemplateItemQuery attendanceSallaryTemplateItemQuery = new AttendanceSallaryTemplateItemQuery();
        attendanceSallaryTemplateItemQuery.setAttendanceSallaryTemplateId(attendanceSallaryTemplateId);
        List<AttendanceSallaryTemplateItemListVO> templateInfoList = attendanceSallaryTemplateItemDAO.findConfigTypeCount(attendanceSallaryTemplateId, tokenUser.getSiteId());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //查错误数据
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(1);
        List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
        //数据
        List<List<String>> dataList = new ArrayList<>();
        for (AttendanceDataTempListVO attendanceDataTempListVO : select) {
            List<String> dataInfoList = new ArrayList<>();
            List<String> strings = JSON.parseArray(attendanceDataTempListVO.getValue(), String.class);
            for (String string : strings) {
                if ("null".equals(string)) {
                    string = " ";
                }
                dataInfoList.add(string);
            }
            dataList.add(dataInfoList);
        }

        //制作表头
        List<List<String>> list = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < 2 + 31 * templateInfoList.size(); i++) {
            ArrayList<String> list1 = new ArrayList<>();
            if (i == 0) {
                list1.add("姓名");
                list1.add("姓名");
            } else if (i == 1) {
                list1.add("手机");
                list1.add("手机");
            } else {
                list1.add((((i - 2) / templateInfoList.size()) + 1) + "");
                list1.add(templateInfoList.get(index).getConfigurationName());
                index = index == templateInfoList.size() - 1 ? 0 : index + 1;
            }
            list.add(list1);
        }
        EasyExcel.write(outputStream)
                .head(list)
                .sheet("考勤工资记录")
                .doWrite(dataList);
        InputStream byteArrayInputStream = new ByteArrayInputStream(outputStream.toByteArray());
        AttendanceDataTempVO attendanceDataTempVO = new AttendanceDataTempVO();
        attendanceDataTempVO.setIs(byteArrayInputStream);
        attendanceDataTempVO.setFileName(attendanceDataRecordVO.getFileName());
        return attendanceDataTempVO;
    }

    @Override
    public AttendanceDataTempVO workHoursInfoById(String attendanceDataTempId, TokenUser tokenUser) {
        AttendanceDataTempVO attendanceDataTempVO1 = selectById(attendanceDataTempId, tokenUser.getSiteId());
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(attendanceDataTempVO1.getAttendanceDataRecordId(), tokenUser.getSiteId());
        String templateId = attendanceDataRecordVO.getAttendanceSallaryTemplateId();
        //查出模板的所有配置项
        AttendanceSallaryTemplateItemQuery attendanceSallaryTemplateItemQuery = new AttendanceSallaryTemplateItemQuery();
        attendanceSallaryTemplateItemQuery.setAttendanceSallaryTemplateId(templateId);
        List<AttendanceSallaryTemplateItemListVO> attendanceSallaryTemplateItemListVO = attendanceSallaryTemplateItemService.select(attendanceSallaryTemplateItemQuery, tokenUser.getSiteId());
        Map<String, AttendanceSallaryTemplateItemListVO> configMap = attendanceSallaryTemplateItemListVO
                .stream().collect(Collectors
                        .toMap(AttendanceSallaryTemplateItemListVO::getConfigurationStatus, p -> p));

        AttendanceDataTempVO attendanceDataTempVO = selectById(attendanceDataTempId, tokenUser.getSiteId());
        EmpPositionQuery empPositionQuery = new EmpPositionQuery();
        empPositionQuery.setEmpId(attendanceDataTempVO.getEmpId());
        List<EmpPositionListVO> select = empPositionService.select(empPositionQuery, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(select) || select.size() == 0) {
            //todo 暂时不校验员工是否有岗位
            // throw new HrException("此员工无岗位，或无此员工");
        } else {
            EmpInfoDO empInfoDO = empInfoService.selectById(select.get(0).getEmpId());
            if (empInfoDO != null) {
                UserVO userVO = userService.selectById(empInfoDO.getUserId());
                if (userVO != null) {
                    attendanceDataTempVO.setAvatar(userVO.getAvatar());
                }
            }
            PositionInfoVO positionInfoVO = positionInfoService.selectById(select.get(0).getPositionId(), tokenUser.getSiteId());
            attendanceDataTempVO.setPositionName(positionInfoVO.getName());
        }
        List<String> list = JSON.parseArray(attendanceDataTempVO.getValue(), String.class);
        //存放工种和工时
        Map<String, Integer> stringIntegerHashMap = new HashMap<>();
        //存放工种和工资
        Map<String, BigDecimal> map = new HashMap<>();
        BigDecimal salary = BigDecimal.ZERO;
        String workType = "";
        String workTime = "";
        //每个工种的工时
        for (int i = 0; i < list.size(); i += 2) {
            if (i > 1) {
                salary = BigDecimal.ZERO;
                if (i + 1 < list.size()) {
                    //计算工资
                    workType = list.get(i + 1);
                    workTime = list.get(i);
                    if (!"null".equals(workType) && !"null".equals(workTime)) {
                        salary = salary.add(configMap.get(workType).getSallaryImpact()
                                .multiply(configMap.get(workTime).getSallaryImpact())
                                .divide(new BigDecimal(100)));

                        if (stringIntegerHashMap.containsKey(list.get(i + 1))) {
                            stringIntegerHashMap.put(list.get(i + 1), stringIntegerHashMap.get(list.get(i + 1)) + 1);
                        } else {
                            stringIntegerHashMap.put(list.get(i + 1), 1);
                        }
                        //工资相加
                        if (map.containsKey(list.get(i + 1))) {
                            map.put(list.get(i + 1), map.get(list.get(i + 1)).add(salary));
                        } else {
                            map.put(list.get(i + 1), salary);
                        }
                    }
                }

            }
        }
        List<String> workTypeList = new ArrayList<>();
        List<Integer> workDayList = new ArrayList<>();
        List<BigDecimal> salaryList = new ArrayList<>();
        for (Map.Entry<String, Integer> entry : stringIntegerHashMap.entrySet()) {
            workTypeList.add(entry.getKey());
            workDayList.add(entry.getValue());
        }
        for (String workType1 : workTypeList) {
            salaryList.add(map.get(workType1));
        }
        int day = 0;
        BigDecimal salary1 = BigDecimal.ZERO;
        for (Integer integer : workDayList) {
            day += integer;
        }
        for (BigDecimal bigDecimal : salaryList) {
            salary1 = salary1.add(bigDecimal);
        }
        List<String> workTypeList1 = new ArrayList<>();
        List<Integer> workDayList1 = new ArrayList<>();
        List<BigDecimal> salaryList1 = new ArrayList<>();
        workTypeList1.add("所有工种");
        workTypeList1.addAll(workTypeList);
        workDayList1.add(day);
        workDayList1.addAll(workDayList);
        salaryList1.add(salary1);
        salaryList1.addAll(salaryList);
        attendanceDataTempVO.setWorkType(workTypeList1);
        attendanceDataTempVO.setWorkDay(workDayList1);
        attendanceDataTempVO.setWorkSalary(salaryList1);

        return attendanceDataTempVO;
    }

    @Override
    public AttendanceDataTempVO getWorkHoursInfoById(String attendanceDataTempId, TokenUser tokenUser) {
        AttendanceDataTempVO attendanceDataTempVO = selectById(attendanceDataTempId, tokenUser.getSiteId());
        EmpPositionQuery empPositionQuery = new EmpPositionQuery();
        empPositionQuery.setEmpId(attendanceDataTempVO.getEmpId());
        List<EmpPositionListVO> select = empPositionService.select(empPositionQuery, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(select) || select.size() == 0) {
            //todo 暂时不校验员工是否有岗位
            // throw new HrException("此员工无岗位，或无此员工");
        } else {
            EmpInfoDO empInfoDO = empInfoService.selectById(select.get(0).getEmpId());
            if (empInfoDO != null) {
                UserVO userVO = userService.selectById(empInfoDO.getUserId());
                if (userVO != null) {
                    attendanceDataTempVO.setAvatar(userVO.getAvatar());
                }
            }
            PositionInfoVO positionInfoVO = positionInfoService.selectById(select.get(0).getPositionId(), tokenUser.getSiteId());
            attendanceDataTempVO.setPositionName(positionInfoVO.getName());
        }
        return attendanceDataTempVO;
    }

    @Override
    public Page<AttendanceDataTempListVO> attendanceDataInfo(AttendanceDataTempQuery attendanceDataTempQuery, TokenUser tokenUser) {
        int worHours = 0;
        Page<AttendanceDataTempListVO> attendanceDataTempListVOPage = selectPage(attendanceDataTempQuery, tokenUser.getSiteId());
        for (AttendanceDataTempListVO entity : attendanceDataTempListVOPage.getEntities()) {
            //查岗位
            AttendanceDataTempVO attendanceDataTempVO = selectById(entity.getEmpId(), tokenUser.getSiteId());
            if (attendanceDataTempVO != null) {
                EmpPositionQuery empPositionQuery = new EmpPositionQuery();
                empPositionQuery.setEmpId(attendanceDataTempVO.getEmpId());
                List<EmpPositionListVO> select1 = empPositionService.select(empPositionQuery, tokenUser.getSiteId());
                if (CollectionUtils.isEmpty(select1) || select1.size() == 0) {
                    //todo 暂时不校验员工是否有岗位
                    // throw new HrException("此员工无岗位，或无此员工");
                } else {
                    PositionInfoVO positionInfoVO = positionInfoService.selectById(select1.get(0).getPositionId(), tokenUser.getSiteId());
                    entity.setPositionName(positionInfoVO.getName());
                }
            }
            worHours = 0;
            List<String> list = JSON.parseArray(entity.getValue(), String.class);
            for (int i = 0; i < list.size(); i = i + 2) {
                if (i > 1 && i + 1 < list.size()) {
                    if (!"null".equals(list.get(i)) && !"null".equals(list.get(i + 1))) {
                        worHours++;
                    }
                }
            }
            entity.setWorkDays(worHours);
            //查看是否归档
            AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(entity.getAttendanceDataRecordId(), tokenUser.getSiteId());
            EmployeeSalaryQuery employeeSalaryQuery = new EmployeeSalaryQuery();
            employeeSalaryQuery.setEmpId(entity.getEmpId());
            employeeSalaryQuery.setSalaryTemplateSalaryIds(attendanceDataRecordVO.getSalaryTemplateSalaryId());
            List<EmployeeSalaryDO> select = employeeSalaryService.select(employeeSalaryQuery, tokenUser);
            if (select != null && select.size() > 0) {
                entity.setIsHandle(select.get(0).getIsArchive());
            }
        }
        return attendanceDataTempListVOPage;
    }

    @Override
    public Page<AttendanceDataTempListVO> getAttendanceDataInfo(AttendanceDataTempQuery attendanceDataTempQuery, TokenUser tokenUser) {
        int worHours = 0;
        Page<AttendanceDataTempListVO> attendanceDataTempListVOPage = selectPage(attendanceDataTempQuery, tokenUser.getSiteId());
        for (AttendanceDataTempListVO entity : attendanceDataTempListVOPage.getEntities()) {
            worHours = 0;
            List<String> maps = JSON.parseArray(entity.getWorkingHours(), String.class);
            for (String map : maps) {
                Map<String, Integer> map1 = (Map) JSON.parse(map);
                for (Map.Entry<String, Integer> stringIntegerEntry : map1.entrySet()) {
                    worHours += stringIntegerEntry.getValue();
                }
            }
            entity.setWorkDays(worHours);
            //查看是否归档
            AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(entity.getAttendanceDataRecordId(), tokenUser.getSiteId());
            EmployeeSalaryQuery employeeSalaryQuery = new EmployeeSalaryQuery();
            employeeSalaryQuery.setEmpId(entity.getEmpId());
            employeeSalaryQuery.setSalaryTemplateSalaryIds(attendanceDataRecordVO.getSalaryTemplateSalaryId());
            List<EmployeeSalaryDO> select = employeeSalaryService.select(employeeSalaryQuery, tokenUser);
            if (select != null && select.size() > 0) {
                entity.setIsHandle(select.get(0).getIsArchive());
            }

        }
        return attendanceDataTempListVOPage;
    }

    @Override
    public Integer newCalculateSalary(String recordId, TokenUser tokenUser) {
        int count = 0;
        AttendanceDataRecordUpdateDTO attendanceDataRecordUpdateDTO = new AttendanceDataRecordUpdateDTO();
        attendanceDataRecordUpdateDTO.setId(recordId);
        attendanceDataRecordUpdateDTO.setIsImport(true);
        attendanceDataRecordService.update(attendanceDataRecordUpdateDTO, tokenUser);

        //查出用工单位的模板信息
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(recordId, tokenUser.getSiteId());
        AttendanceSallaryTemplateQuery attendanceSallaryTemplateQuery = new AttendanceSallaryTemplateQuery();
        attendanceSallaryTemplateQuery.setCompanyId(attendanceDataRecordVO.getCompanyId());
        List<AttendanceSallaryTemplateListVO> select = attendanceSalaryTemplateService.select(attendanceSallaryTemplateQuery, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(select)) {
            throw new ParamException("无模板");
        }
        AttendanceSallaryTemplateItemQuery attendanceSallaryTemplateItemQuery = new AttendanceSallaryTemplateItemQuery();
        attendanceSallaryTemplateItemQuery.setAttendanceSallaryTemplateId(select.get(0).getId());
        List<AttendanceSallaryTemplateItemListVO> attendanceSalaryTemplateItemListVO = attendanceSallaryTemplateItemService.select(attendanceSallaryTemplateItemQuery, tokenUser.getSiteId());
        Map<String, AttendanceSallaryTemplateItemListVO> map = attendanceSalaryTemplateItemListVO.stream()
                .collect(Collectors.toMap(AttendanceSallaryTemplateItemListVO::getConfigurationStatus, p -> p));
        //记录中所有正确数据
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(0);
        List<AttendanceDataTempListVO> attendanceDataTempListVO = select(attendanceDataTempQuery, tokenUser.getSiteId());
        if (CollectionUtils.isEmpty(attendanceDataTempListVO)) {
            throw new ParamException("导入记录没有正确数据");
        }
        //正确数据计算工资
        BigDecimal salary = null;
        //工种、考勤
        String workType = "";
        String workTime = "";
        //
        for (AttendanceDataTempListVO dataTempListVO : attendanceDataTempListVO) {
            salary = BigDecimal.ZERO;
            List<String> list = JSON.parseArray(dataTempListVO.getValue(), String.class);
            for (int i = 0; i < list.size(); i += 2) {
                if (i > 1) {
                    if (i + 1 < list.size()) {
                        workType = list.get(i + 1);
                        workTime = list.get(i);
                        if (!"null".equals(workTime) && !"null".equals(workType)) {
                            salary = salary.add(map.get(workType).getSallaryImpact()
                                    .multiply(map.get(workTime).getSallaryImpact())
                                    .divide(new BigDecimal(100)));
                        }
                    }
                }
            }
            AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
            attendanceDataTempUpdateDTO.setId(dataTempListVO.getId());
            attendanceDataTempUpdateDTO.setSallaryImpact(salary);
            count += update(attendanceDataTempUpdateDTO, tokenUser);
        }
        return count;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer calculateSalary(String recordId, TokenUser tokenUser) {
        int count = 0;
        AttendanceDataRecordUpdateDTO attendanceDataRecordUpdateDTO = new AttendanceDataRecordUpdateDTO();
        attendanceDataRecordUpdateDTO.setId(recordId);
        attendanceDataRecordUpdateDTO.setIsImport(true);
        attendanceDataRecordService.update(attendanceDataRecordUpdateDTO, tokenUser);
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(recordId);
        attendanceDataTempQuery.setIsError(0);
        List<AttendanceDataTempDO> attendanceDataTempDOS = selectDO(attendanceDataTempQuery, tokenUser.getSiteId());
        AttendanceSallaryTemplateItemDO attendanceSallaryTemplateItemDO = new AttendanceSallaryTemplateItemDO();
        //考勤工资模板信息
        //   List<AttendanceSallaryTemplateItemListVO> attendanceSallaryTemplateItemDOS = null;
        int workTypeIndex = 0;
        //模板的配置名称列表
        List<String> configNameList = new ArrayList<>();
        Map<String, Integer> configNameMap = new LinkedHashMap<>();

        List<AttendanceSallaryTemplateItemListVO> attendanceSallaryTemplateItemListVOS = new ArrayList<>();
        //工种信息Map
        List<AttendanceSallaryTemplateItemListVO> templateInfoList = null;
        List<AttendanceSallaryTemplateItemListVO> templateInfoList1 = null;
        for (AttendanceDataTempDO attendanceDataTempDO : attendanceDataTempDOS) {
            //工资影响
            BigDecimal salary = BigDecimal.ZERO;
            Map<String, Integer> workTypeInfo = new LinkedHashMap<>();
            String value = attendanceDataTempDO.getValue();
            if (templateInfoList == null) {
                AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.selectById(attendanceDataTempDO.getAttendanceDataRecordId(), tokenUser.getSiteId());
                if (attendanceDataRecordVO != null) {
                    templateInfoList = attendanceSallaryTemplateItemDAO.findConfigTypeCount(attendanceDataRecordVO.getAttendanceSallaryTemplateId(), tokenUser.getSiteId());
                    templateInfoList1 = attendanceSallaryTemplateItemDAO.selectBySort(attendanceDataRecordVO.getAttendanceSallaryTemplateId(), tokenUser.getSiteId());
                    for (AttendanceSallaryTemplateItemListVO attendanceSallaryTemplateItemListVO : templateInfoList) {
                        configNameMap.put(attendanceSallaryTemplateItemListVO.getConfigurationName(), 0);
                    }

                    for (String key : configNameMap.keySet()) {
                        configNameList.add(key);
                    }

                    attendanceSallaryTemplateItemListVOS = attendanceSallaryTemplateItemService.getConfigByTempId(attendanceDataRecordVO.getAttendanceSallaryTemplateId(), tokenUser);
                    //找到工种索引
                    for (int i = 0; i < configNameList.size(); i++) {
                        if ("工种".equals(configNameList.get(i))) {
                            workTypeIndex = i;
                            break;
                        }
                    }
                }

            }
            //计算工资影响
            List<String> strings = JSON.parseArray(value, String.class);
            if ((strings.size() - 2) / 31 != configNameList.size() || (strings.size() - 2) % 31 != 0) {
                throw new ParamException("考勤模板字段数和导入数据数量不符合");
            }
            List<String> strings1 = new ArrayList<>();
            for (int i = 0; i < strings.size(); i++) {
                if (i > 1) {
                    if ((i - 2) % configNameList.size() == workTypeIndex && !"null".equals(strings.get(i))) {
                        Integer integer = workTypeInfo.get(strings.get(i));
                        if (integer != null) {
                            workTypeInfo.put(strings.get(i), integer + 1);
                        } else {
                            workTypeInfo.put(strings.get(i), 1);
                        }
                    }
                    String data = strings.get(i);
                    String configName = configNameList.get((i - 2) % configNameList.size());

                    for (AttendanceSallaryTemplateItemListVO attendanceSallaryTemplateItemListVO : templateInfoList1) {
                        if (configName.equals(attendanceSallaryTemplateItemListVO.getConfigurationName())) {
                            if (data.equals(attendanceSallaryTemplateItemListVO.getConfigurationStatus())) {
                                salary = salary.add(attendanceSallaryTemplateItemListVO.getSallaryImpact());
                                break;
                            }
                        }
                    }
                }

            }
            for (Map.Entry<String, Integer> stringIntegerEntry : workTypeInfo.entrySet()) {
                strings1.add(JSON.toJSONString(stringIntegerEntry));
            }
            AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
            attendanceDataTempUpdateDTO.setId(attendanceDataTempDO.getId());
            attendanceDataTempUpdateDTO.setSallaryImpact(salary);
            attendanceDataTempUpdateDTO.setWorkingHours(JSON.toJSONString(strings1));
            count = count + update(attendanceDataTempUpdateDTO, tokenUser);
        }
        return count;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer handleError(AttendanceDataTempQuery attendanceDataTempQuery, TokenUser tokenUser) {
        int count = 0;
        if (!StringUtils.isEmpty(attendanceDataTempQuery.getEmpName()) && !StringUtils.isEmpty(attendanceDataTempQuery.getEmpPhone())) {
            EmpInfoAddDTO empInfoAddDTO = new EmpInfoAddDTO();
            empInfoAddDTO.setName(attendanceDataTempQuery.getEmpName());
            empInfoAddDTO.setPhone(attendanceDataTempQuery.getEmpPhone());
            empInfoAddDTO.setCompanyId(attendanceDataTempQuery.getCompanyId());
            EmpInfoVO save = empInfoService.save(empInfoAddDTO, tokenUser);
            if (save != null) {
                count++;
            }
        } else if (attendanceDataTempQuery.getAttendanceDataTempIds() != null && attendanceDataTempQuery.getAttendanceDataTempIds().length > 1) {
            count = deleteByIds(tokenUser, attendanceDataTempQuery.getAttendanceDataTempIds());
        } else if (attendanceDataTempQuery.getAttendanceDataTempIds() != null && attendanceDataTempQuery.getAttendanceDataTempIds().length == 1) {
            AttendanceDataTempVO attendanceDataTempVO = selectById(attendanceDataTempQuery.getAttendanceDataTempIds()[0], tokenUser.getSiteId());
            count = deleteByIds(tokenUser, attendanceDataTempQuery.getAttendanceDataTempIds());
            if (attendanceDataTempVO != null) {
                AttendanceDataTempQuery attendanceDataTempQuery1 = new AttendanceDataTempQuery();
                attendanceDataTempQuery1.setEmpId(attendanceDataTempVO.getEmpId());
                List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery1, tokenUser.getSiteId());
                //重复数据删除，另一条重复需要改为正确标志
                if (select.size() == 1) {
                    AttendanceDataTempListVO attendanceDataTempListVO = select.get(1);
                    AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
                    attendanceDataTempUpdateDTO.setId(attendanceDataTempListVO.getId());
                    attendanceDataTempUpdateDTO.setIsError(false);
                    update(attendanceDataTempUpdateDTO, tokenUser);
                }
            }
        }
        return count;
    }

    @Override
    public AttendanceDataAllAndErrorVO getAttendanceDataTempAllAndError(AttendanceDataTempQuery attendanceDataTempQuery, TokenUser tokenUser) {
        AttendanceDataAllAndErrorVO attendanceDataAllAndErrorVO = new AttendanceDataAllAndErrorVO();
        attendanceDataAllAndErrorVO.setAttendanceDataRecordId(attendanceDataTempQuery.getRecordId());
        //表头数据
        List<AttendanceSallaryTemplateItemListVO> config = attendanceSallaryTemplateItemService.getConfigByTempId(attendanceDataTempQuery.getTempId(), tokenUser);
        List<String> headData = new ArrayList<>();
        for (AttendanceSallaryTemplateItemListVO attendanceSallaryTemplateItemListVO : config) {
            headData.add(attendanceSallaryTemplateItemListVO.getConfigurationName());
        }
        attendanceDataAllAndErrorVO.setHeadData(headData);
        //全部数据
        AttendanceDataTempQuery attendanceDataTempQuery1 = new AttendanceDataTempQuery();
        attendanceDataTempQuery1.setPageNo(attendanceDataTempQuery.getPageNo());
        attendanceDataTempQuery1.setPageSize(attendanceDataTempQuery.getPageSize());
        attendanceDataTempQuery1.setRecordId(attendanceDataTempQuery.getRecordId());
        Page<AttendanceDataTempListVO> page1 = selectPage(attendanceDataTempQuery1, tokenUser.getSiteId());
        List<List<String>> allData = new ArrayList<>();
        for (AttendanceDataTempListVO entity : page1.getEntities()) {
            List<String> strings = JSON.parseArray(entity.getValue(), String.class);
            List<String> list = new ArrayList<>();
            for (String string : strings) {
                Map<String, Integer> map = new HashMap<>();
                map.put(string, 0);
                String s = JSONObject.toJSONString(map);
                list.add(s);
            }
            allData.add(list);
        }
        attendanceDataAllAndErrorVO.setAllData(allData);
        attendanceDataAllAndErrorVO.setAllDataCount(page1.getEntityCount());
        attendanceDataAllAndErrorVO.setAllDataPageCount(page1.getPageCount());

        //错误数据
        attendanceDataTempQuery1.setIsError(1);
        Page<AttendanceDataTempListVO> page2 = selectPage(attendanceDataTempQuery1, tokenUser.getSiteId());
        List<List<String>> errorData = new ArrayList<>();
        for (AttendanceDataTempListVO entity : page2.getEntities()) {
            List<String> mapList = new ArrayList<>();
            List<String> strings = JSON.parseArray(entity.getValue(), String.class);
            for (String string : strings) {
                Map<String, Integer> stringIntegerHashMap = new HashMap<>();
                //所有数据先都初始化，再修改字段不符合的数据
                stringIntegerHashMap.put(string, 0);
                mapList.add(JSONObject.toJSONString(stringIntegerHashMap));
            }
            //错误原因加到最后
            Map<String, Integer> stringIntegerHashMap1 = new HashMap<>();
            stringIntegerHashMap1.put(entity.getErrorReason(), 1);
            mapList.add(JSONObject.toJSONString(stringIntegerHashMap1));
            //数据ID加到最后
            Map<String, Integer> stringIntegerHashMap2 = new HashMap<>();
            stringIntegerHashMap2.put(entity.getId(), 0);
            mapList.add(JSONObject.toJSONString(stringIntegerHashMap2));

            if ("字段无法匹配".equals(entity.getErrorReason())) {
                String[] errorColString = entity.getErrorCol().split(",");
                for (String colString : errorColString) {
                    int col = Integer.parseInt(colString);
                    String key = "";
                    String s = mapList.get(col);
                    Map<String, Integer> stringIntegerMap = (Map) JSONObject.parseObject(s);
                    for (Map.Entry<String, Integer> stringIntegerEntry : stringIntegerMap.entrySet()) {
                        key = stringIntegerEntry.getKey();
                    }
                    HashMap<String, Integer> hashMap = new HashMap<>();
                    hashMap.put(key, 1);
                    mapList.set(col, JSONObject.toJSONString(hashMap));
                }
            }
            errorData.add(mapList);
        }
        attendanceDataAllAndErrorVO.setErrorData(errorData);
        attendanceDataAllAndErrorVO.setErrorDataCount(page2.getEntityCount());
        attendanceDataAllAndErrorVO.setErrorDataPageCount(page2.getPageCount());
        return attendanceDataAllAndErrorVO;
    }

    @Override
    public AttendanceDataAllAndErrorVO newImportFile(MultipartFile file, AttendanceDataTempQuery attendanceDataTempQuery1, TokenUser tokenUser) {
        //添加解析数据记录
        AttendanceDataRecordAddDTO attendanceDataRecordAddDTO = new AttendanceDataRecordAddDTO();
        attendanceDataRecordAddDTO.setCompanyId(attendanceDataTempQuery1.getCompanyId());
        attendanceDataRecordAddDTO.setFileName(file.getOriginalFilename());
        attendanceDataRecordAddDTO.setYear(attendanceDataTempQuery1.getYear());
        attendanceDataRecordAddDTO.setMonth(attendanceDataTempQuery1.getMonth());
        attendanceDataRecordAddDTO.setDataType("考勤工资记录");
        attendanceDataRecordAddDTO.setIsImport(false);
        attendanceDataRecordAddDTO.setIsHandle(false);
        attendanceDataRecordAddDTO.setAttendanceSallaryTemplateId(attendanceDataTempQuery1.getTempId());
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.save(attendanceDataRecordAddDTO, tokenUser);
        AttendPunchSalaryNoModelDataListener attendPunchNoModelDataListener = new AttendPunchSalaryNoModelDataListener();
        try {
            EasyExcel.read(file.getInputStream(), attendPunchNoModelDataListener).sheet().doRead();
        } catch (Exception e) {
            new HrException("文件上传失败");
        }

        List<Map<Integer, String>> dataList = attendPunchNoModelDataListener.getList();
        for (Map<Integer, String> map : dataList) {
            ArrayList<String> list = new ArrayList<>();
            AttendanceDataTempAddDTO attendanceDataTempAddDTO = new AttendanceDataTempAddDTO();
            attendanceDataTempAddDTO.setAttendanceDataRecordId(attendanceDataRecordVO.getId());
            attendanceDataTempAddDTO.setIsError(false);
            String errorCol = "";
            if (map.get(0) == null) {
                attendanceDataTempAddDTO.setErrorReason("名字为空");
                attendanceDataTempAddDTO.setIsError(true);
            } else if (map.get(1) == null) {
                attendanceDataTempAddDTO.setErrorReason("手机号为空");
                attendanceDataTempAddDTO.setIsError(true);
            }
            EmpInfoDO empInfoDO = empInfoService.selectByPhone(map.get(1), tokenUser);
            if (!attendanceDataTempAddDTO.getIsError()) {
                //查询是否有此员工
                if (empInfoDO == null) {
                    attendanceDataTempAddDTO.setErrorReason("系统内无此人");
                    attendanceDataTempAddDTO.setIsError(true);
                    attendanceDataTempAddDTO.setEmpName(map.get(0));
                    attendanceDataTempAddDTO.setEmpPhone(map.get(1));
                } else {
                    attendanceDataTempAddDTO.setEmpId(empInfoDO.getId());
                    attendanceDataTempAddDTO.setEmpName(empInfoDO.getName());
                    attendanceDataTempAddDTO.setEmpPhone(empInfoDO.getPhone());
                }
            }
            if (!attendanceDataTempAddDTO.getIsError()) {
                //查询是否重复数据
                AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
                attendanceDataTempQuery.setRecordId(attendanceDataRecordVO.getId());
                attendanceDataTempQuery.setEmpPhone(map.get(1));
                List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
                if (!CollectionUtils.isEmpty(select)) {
                    //把第一个数据也改成重复数据
                    AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
                    attendanceDataTempUpdateDTO.setId(select.get(0).getId());
                    attendanceDataTempUpdateDTO.setIsError(true);
                    attendanceDataTempUpdateDTO.setErrorReason("表格数据重复");
                    update(attendanceDataTempUpdateDTO, tokenUser);

                    attendanceDataTempAddDTO.setErrorReason("表格数据重复");
                    attendanceDataTempAddDTO.setIsError(true);
                }
            }

            //拿到所有字段并逐个匹配
            for (int i = 0; i < map.size(); i++) {
                if (i > 1) {
                    AttendanceSallaryTemplateItemQuery attendanceSallaryTemplateItemQuery = new AttendanceSallaryTemplateItemQuery();
                    attendanceSallaryTemplateItemQuery.setCompanyId(attendanceDataTempQuery1.getCompanyId());
                    attendanceSallaryTemplateItemQuery.setConfigurationStatus(map.get(i));
                    if (i % 2 == 1) {
                        attendanceSallaryTemplateItemQuery.setConfigurationName("工种");
                    } else {
                        attendanceSallaryTemplateItemQuery.setConfigurationName("考勤");
                    }
                    List<AttendanceSallaryTemplateItemListVO> select = attendanceSallaryTemplateItemService.select(attendanceSallaryTemplateItemQuery, tokenUser.getSiteId());
                    if (CollectionUtils.isEmpty(select)) {
                        errorCol = errorCol + i + ",";
                    }
                }
                if (map.get(i) == null) {
                    list.add("null");
                } else {
                    list.add(map.get(i));
                }
            }
            if (!attendanceDataTempAddDTO.getIsError()) {
                if (!StringUtils.isEmpty(errorCol)) {
                    attendanceDataTempAddDTO.setErrorCol(errorCol);
                    attendanceDataTempAddDTO.setErrorReason("字段无法匹配");
                    attendanceDataTempAddDTO.setIsError(true);
                }
            }
            attendanceDataTempAddDTO.setValue(JSONArray.toJSONString(list));
            attendanceDataTempAddDTO.setIsHandle(false);
            save(attendanceDataTempAddDTO, tokenUser);
        }
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();

        AttendanceDataAllAndErrorVO attendanceDataAllAndErrorVO = new AttendanceDataAllAndErrorVO();
        attendanceDataAllAndErrorVO.setFileName(file.getOriginalFilename());
        attendanceDataTempQuery.setRecordId(attendanceDataRecordVO.getId());
        attendanceDataAllAndErrorVO.setAllDataCount(selectCount(attendanceDataTempQuery, tokenUser.getSiteId()));
        attendanceDataTempQuery.setIsError(1);
        attendanceDataAllAndErrorVO.setErrorDataCount(selectCount(attendanceDataTempQuery, tokenUser.getSiteId()));
        attendanceDataAllAndErrorVO.setAttendanceDataRecordId(attendanceDataRecordVO.getId());
        return attendanceDataAllAndErrorVO;

    }

    @Override
    public AttendanceDataAllAndErrorVO importFile(MultipartFile file, AttendanceDataTempQuery attendanceDataTempQuery1, TokenUser tokenUser) {
        //添加解析数据记录
        AttendanceDataRecordAddDTO attendanceDataRecordAddDTO = new AttendanceDataRecordAddDTO();
        attendanceDataRecordAddDTO.setCompanyId(attendanceDataTempQuery1.getCompanyId());
        attendanceDataRecordAddDTO.setFileName(file.getOriginalFilename());
        attendanceDataRecordAddDTO.setYear(attendanceDataTempQuery1.getYear());
        attendanceDataRecordAddDTO.setMonth(attendanceDataTempQuery1.getMonth());
        attendanceDataRecordAddDTO.setDataType("考勤工资记录");
        attendanceDataRecordAddDTO.setIsHandle(false);
        attendanceDataRecordAddDTO.setAttendanceSallaryTemplateId(attendanceDataTempQuery1.getTempId());
        AttendanceDataRecordVO attendanceDataRecordVO = attendanceDataRecordService.save(attendanceDataRecordAddDTO, tokenUser);
        AttendPunchNoModelDataListener attendPunchNoModelDataListener = new AttendPunchNoModelDataListener();
        try {
            EasyExcel.read(file.getInputStream(), attendPunchNoModelDataListener).sheet().doRead();
        } catch (Exception e) {
            new HrException("文件上传失败");
        }
        int col = attendPunchNoModelDataListener.getCol();
        //不匹配字段列数集合
        String errorCol = "";
        List<Map<Integer, String>> list = attendPunchNoModelDataListener.getList();
        List<String> dataList = attendPunchNoModelDataListener.getDataList();
        //从第二个数据进行操作
        for (int i = 0; i < list.size(); i++) {
            List<String> stringList = new ArrayList<>();
            errorCol = "";
            //查是否存在该用户
            AttendanceDataTempAddDTO attendanceDataTempAddDTO = new AttendanceDataTempAddDTO();
            attendanceDataTempAddDTO.setAttendanceDataRecordId(attendanceDataRecordVO.getId());
            for (int i1 = 0; i1 < col; i1++) {
                String s = list.get(i).get(i1);
                if (StringUtils.isEmpty(s)) {
                    s = "null";
                }
                stringList.add(s);
            }
            attendanceDataTempAddDTO.setValue(JSONArray.toJSONString(stringList));

            EmpInfoDO empInfoDO = empInfoService.selectByPhone(list.get(i).get(1), tokenUser);
            if (empInfoDO == null) {
                attendanceDataTempAddDTO.setIsError(true);
                attendanceDataTempAddDTO.setErrorReason("系统内无此人");
            } else {
                attendanceDataTempAddDTO.setEmpId(empInfoDO.getId());
                attendanceDataTempAddDTO.setEmpName(empInfoDO.getName());
                attendanceDataTempAddDTO.setEmpPhone(empInfoDO.getPhone());
                //查是否重复数据
                AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
                attendanceDataTempQuery.setEmpId(empInfoDO.getId());
                attendanceDataTempQuery.setRecordId(attendanceDataRecordVO.getId());
                List<AttendanceDataTempListVO> select = select(attendanceDataTempQuery, tokenUser.getSiteId());
                if (select.size() > 0) {
                    AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO = new AttendanceDataTempUpdateDTO();
                    attendanceDataTempUpdateDTO.setId(select.get(0).getId());
                    attendanceDataTempUpdateDTO.setIsError(true);
                    attendanceDataTempUpdateDTO.setErrorReason("表格数据重复");
                    update(attendanceDataTempUpdateDTO, tokenUser);
                    //未被标记错误的数据再标记.复制属性
                    AttendanceDataTempListVO attendanceDataTempListVO1 = select.get(0);
                    if (!StringUtils.isEmpty(attendanceDataTempListVO1.getErrorCol())) {
                        attendanceDataTempAddDTO.setErrorCol(attendanceDataTempListVO1.getErrorCol());
                    }
                    if (attendanceDataTempListVO1.getIsHandle() != null) {
                        attendanceDataTempAddDTO.setIsHandle(attendanceDataTempListVO1.getIsHandle());
                    }
                    attendanceDataTempAddDTO.setIsError(true);
                    attendanceDataTempAddDTO.setErrorReason("表格数据重复");
                } else {
                    //查表格数据是否对应
                    Map<Integer, String> integerStringMap = list.get(i);
                    for (int i1 = 0; i1 < integerStringMap.size(); i1++) {
                        if (i1 > 1) {
                            String configStatus = integerStringMap.get(i1);
                            if (!StringUtils.isEmpty(configStatus)) {
                                String configName = dataList.get(i1);
                                AttendanceSallaryTemplateItemQuery attendanceSallaryTemplateItemQuery = new AttendanceSallaryTemplateItemQuery();
                                attendanceSallaryTemplateItemQuery.setConfigurationName(configName);
                                attendanceSallaryTemplateItemQuery.setAttendanceSallaryTemplateId(attendanceDataTempQuery1.getTempId());
                                List<AttendanceSallaryTemplateItemListVO> select1 = attendanceSallaryTemplateItemService.select(attendanceSallaryTemplateItemQuery, tokenUser.getSiteId());
                                for (int i2 = 0; i2 < select1.size(); i2++) {
                                    if (select1.get(i2).getConfigurationStatus().equals(configStatus)) {
                                        break;
                                    }
                                    if (select1.size() == i2 + 1) {
                                        attendanceDataTempAddDTO.setIsError(true);
                                        attendanceDataTempAddDTO.setErrorReason("字段无法匹配");
                                        errorCol = errorCol + i1 + ",";
                                    }
                                }
                                if (!StringUtils.isEmpty(errorCol)) {
                                    attendanceDataTempAddDTO.setErrorCol(errorCol);
                                }
                            }

                        }
                    }
                }
            }
            save(attendanceDataTempAddDTO, tokenUser);
        }
        //分页查询数据解析后的数据
        AttendanceDataTempQuery attendanceDataTempQuery = new AttendanceDataTempQuery();
        attendanceDataTempQuery.setRecordId(attendanceDataRecordVO.getId());
        attendanceDataTempQuery.setTempId(attendanceDataTempQuery1.getTempId());
        attendanceDataTempQuery.setPageNo(attendanceDataTempQuery1.getPageNo());
        attendanceDataTempQuery.setPageSize(attendanceDataTempQuery1.getPageSize());
        return getAttendanceDataTempAllAndError(attendanceDataTempQuery, tokenUser);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceDataTempVO save(AttendanceDataTempAddDTO attendanceDataTempAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceDataTempDO attendanceDataTempDO = attendanceDataTempAddDTO.to(AttendanceDataTempDO.class);
        // 保存
        attendanceDataTempDO = save(attendanceDataTempDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return attendanceDataTempDO.to(AttendanceDataTempVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceDataTempUpdateDTO attendanceDataTempUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceDataTempDO attendanceDataTempDO = attendanceDataTempUpdateDTO.to(AttendanceDataTempDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceDataTempDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceDataTempIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceDataTempIds);
    }


    @Override
    public AttendanceDataTempVO selectById(String attendanceDataTempId, String siteId) {
        AttendanceDataTempDO attendanceDataTempDO = selectDOById(attendanceDataTempId, siteId);
        if (attendanceDataTempDO == null) {
            return null;
        }
        return attendanceDataTempDO.to(AttendanceDataTempVO.class);
    }

    @Override
    public List<AttendanceDataTempListVO> select(AttendanceDataTempQuery attendanceDataTempQuery, String siteId) {
        List<AttendanceDataTempDO> attendanceDataTempDOList = selectDO(attendanceDataTempQuery, siteId);
        if (CollectionUtils.isEmpty(attendanceDataTempDOList)) {
            return attendanceDataTempDOList == null ? null : new ArrayList<>();
        }
        return attendanceDataTempDOList.stream()
                .map(attendanceDataTempDO -> attendanceDataTempDO.to(AttendanceDataTempListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceDataTempQuery attendanceDataTempQuery, String siteId) {
        Example example = buildExample(attendanceDataTempQuery, siteId);
        return attendanceDataTempDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceDataTempListVO> selectPage(AttendanceDataTempQuery attendanceDataTempQuery, String siteId) {
        Example example = buildExample(attendanceDataTempQuery, siteId);
        Page<AttendanceDataTempDO> page = attendanceDataTempDAO.selectPageByExample(example,
                attendanceDataTempQuery.getPageNo(),
                attendanceDataTempQuery.getPageSize());

        return page.to(AttendanceDataTempListVO.class);
    }

    @Override
    public AttendanceDataTempDO selectDOById(String attendanceDataTempId, String siteId) {
        return listById(attendanceDataTempId, siteId);
    }

    @Override
    public List<AttendanceDataTempDO> selectDO(AttendanceDataTempQuery attendanceDataTempQuery, String siteId) {
        Example example = buildExample(attendanceDataTempQuery, siteId);
        return attendanceDataTempDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param attendanceDataTempQuery 查询参数
     * @param siteId                  所属站点id
     * @return example
     */
    private Example buildExample(AttendanceDataTempQuery attendanceDataTempQuery, String siteId) {
        Example example = new Example(AttendanceDataTempDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        // 排序
        if (!StringUtils.isEmpty(attendanceDataTempQuery.getEmpId())) {
            example.and().andEqualTo("empId", attendanceDataTempQuery.getEmpId());
        }
        if (!StringUtils.isEmpty(attendanceDataTempQuery.getRecordId())) {
            example.and().andEqualTo("attendanceDataRecordId", attendanceDataTempQuery.getRecordId());
        }
        if (!StringUtils.isEmpty(attendanceDataTempQuery.getEmpName())) {
            example.and().andLike("empName", "%" + attendanceDataTempQuery.getEmpName() + "%");
        }
        if (attendanceDataTempQuery.getIsError() != null) {
            example.and().andEqualTo("isError", attendanceDataTempQuery.getIsError());
        }
        if (!StringUtils.isEmpty(attendanceDataTempQuery.getEmpPhone())) {
            example.and().andEqualTo("empPhone", attendanceDataTempQuery.getEmpPhone());
        }
        ExampleKit.setExampleOrder(example, attendanceDataTempQuery.getOrders());
        return example;
    }
}




